﻿using MySql.Data.MySqlClient;
using System.Configuration;

namespace Service.common
{
    /// <summary>
    /// MySql 连接
    /// </summary>
    public class MySqlConnect
    {
        /// <summary>
        /// 获取数据库连接信息
        /// </summary>
        /// <returns>数据库连接</returns>
        public string connection()
        {
            return ConfigurationManager.ConnectionStrings["Service.Properties.Settings.ConStr"].ConnectionString;
        }

        /// <summary>
        /// 一个有效的数据库连接对象
        /// </summary>
        /// <param name="conStr"></param>
        /// <returns>MySqlConnection</returns>
        public MySqlConnection getConnection(string conStr = null)
        {
            MySqlConnection Connection = new MySqlConnection(conStr == null ? new MySqlConnect().connection() : conStr);
            if (Connection.State == System.Data.ConnectionState.Closed)
            {
                Connection.Open();
            }
            return Connection;
        }
    }
}